# include <stdio.h>
# include "uthash.h"

struct hashTable {
  int k;
  int v;
  UT_hash_handle hh;

};

struct hashTable* hashtable;

struct hashTable* find(int key) {
  // printf("find key:%d\n", key);
  struct hashTable* tmp;
  HASH_FIND_INT(hashtable, &key, tmp);
  return tmp;
}

void insert(int key, int val) {
  // printf("insert key:%d val:%d\n", key, val);
  struct hashTable* it = find(key);
  if(it == NULL) {
    struct hashTable* tmp = malloc(sizeof(struct hashTable));
    tmp->k = key; tmp->v = val;
    HASH_ADD_INT(hashtable, k, tmp);
  } else {
    it->v = val;
  }
}

int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
  // printf("twoSum numsSize:%d target:%d \n", numsSize, target);
  hashtable = NULL;
  for(int size = 0; size < numsSize; size++) {
    printf("for size:%d  numsSize:%d \n", size, nums[size]);
    struct hashTable* it = find(target - nums[size]);
    if(it != NULL) {
      int* ret = malloc(sizeof(int) * 2);
      ret[0] = it->v, ret[1] = size;
      *returnSize = 2;
      printf("twoSum returnSize:%d \n", *returnSize);
      return ret;
    }
    insert(nums[size], size);
  }
  *returnSize = 0;
  return NULL;
}


int main() {
  // printf("Demo \n");

  int test1[] = {2, 7, 11, 15};
  int numsSize = 4;
  int target = 9;
  int returnSize = 0;
  printf(" init \n");
  int* result1 = twoSum(test1, numsSize, target, &returnSize);
  printf("resultSize is %d \n", returnSize);
  // printf("result1 %d %d \n", result1[0], result1[1]);

  if(returnSize == 0) {
    printf("resultSize is 0 ");
  } else {
    printf("twoSum result1 %d %d \n", result1[0], result1[1]);
  }


}
